/**

 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 *
 * 项目名：cards-web 
 * 文件名：:UnionPayUtil.java
 * 模块说明：	
 * 修改历史：
 * 2017年10月9日 - liyang - 创建。
 *
 */
package com.thor.palmmall.util.pay;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.thor.palmmall.impl.pay.UnionPayConfig;
import com.thor.palmmall.rest.user.AChannelContents;
import com.unionpay.acp.sdk.HttpClient;
import com.unionpay.acp.sdk.LogUtil;
import com.unionpay.acp.sdk.SDKConfig;
import com.unionpay.acp.sdk.SDKUtil;

/**
 * @author:liyang
 * @time:2017年10月9日 上午10:27:51
 * @since:1.0
 */
public class UnionPayUtil {
  public static void initUnionPayConfig(UnionPayConfig config) {

    Properties properties = new Properties();
    properties.setProperty("acpsdk.frontTransUrl", config.getFrontRequestUrl());
    properties.setProperty("acpsdk.backTransUrl", config.getBackRequestUrl());
    properties.setProperty("acpsdk.singleQueryUrl", config.getSingleQueryUrl());
    properties.setProperty("acpsdk.batchTransUrl", config.getBatchTransUrl());
    properties.setProperty("acpsdk.cardTransUrl", config.getCardRequestUrl());
    properties.setProperty("acpsdk.appTransUrl", config.getAppRequestUrl());

    properties.setProperty("acpsdk.jfFrontTransUrl", config.getJfFrontRequestUrl());
    properties.setProperty("acpsdk.jfBackTransUrl", config.getJfBackRequestUrl());
    properties.setProperty("acpsdk.jfSingleQueryUrl", config.getJfSingleQueryUrl());
    properties.setProperty("acpsdk.jfCardTransUrl", config.getJfCardRequestUrl());
    properties.setProperty("acpsdk.jfAppTransUrl", config.getJfAppRequestUrl());

    properties.setProperty("acpsdk.version", config.getVersion());
    properties.setProperty("acpsdk.signMethod", config.getSignMethod());
    properties.setProperty("acpsdk.ifValidateCNName", String.valueOf(config.isIfValidateCNName()));
    properties.setProperty("acpsdk.ifValidateRemoteCert",
        String.valueOf(config.isIfValidateRemoteCert()));
    properties.setProperty("acpsdk.backUrl", config.getBackUrl());
    properties.setProperty("acpsdk.frontUrl", config.getFrontUrl());

    properties.setProperty("acpsdk.signCert.path", config.getSignCertPath());
    properties.setProperty("acpsdk.signCert.pwd", config.getSignCertPwd());
    properties.setProperty("acpsdk.signCert.type", config.getSignCertType());
    properties.setProperty("acpsdk.encryptCert.path", config.getEncryptCertPath());
    properties.setProperty("acpsdk.rootCert.path", config.getRootCertPath());
    properties.setProperty("acpsdk.middleCert.path", config.getMiddleCertPath());
    SDKConfig.getConfig().loadProperties(properties);
  }

  public static void initUnionPayConfigByChannel(AChannelContents channel) {
    UnionPayConfig config = new UnionPayConfig();

    config.setFrontRequestUrl(channel.getFrontRequestUrl());
    config.setBackRequestUrl(channel.getBackRequestUrl());
    config.setSingleQueryUrl(channel.getSingleQueryUrl());
    config.setBatchQueryUrl(channel.getBatchQueryUrl());
    config.setBatchTransUrl(channel.getBatchTransUrl());
    config.setFileTransUrl(channel.getFileTransUrl());
    config.setAppRequestUrl(channel.getAppRequestUrl());
    config.setCardRequestUrl(channel.getCardRequestUrl());

    config.setJfFrontRequestUrl(channel.getJfFrontRequestUrl());
    config.setJfBackRequestUrl(channel.getJfBackRequestUrl());
    config.setJfSingleQueryUrl(channel.getJfSingleQueryUrl());
    config.setJfCardRequestUrl(channel.getJfCardRequestUrl());
    config.setJfAppRequestUrl(channel.getJfAppRequestUrl());

    config.setVersion(channel.getVersion());
    config.setSignMethod(channel.getSignMethod());
    config.setIfValidateCNName(channel.isIfValidateCNName());
    config.setIfValidateRemoteCert(channel.isIfValidateRemoteCert());
    config.setBackUrl(channel.getBackUrl());
    config.setFrontUrl(channel.getFrontUrl());

    config.setSignCertPath(channel.getSignCertPath());
    config.setSignCertPwd(channel.getSignCertPwd());
    config.setSignCertType(channel.getSignCertType());
    config.setEncryptCertPath(channel.getEncryptCertPath());
    config.setMiddleCertPath(channel.getMiddleCertPath());
    config.setRootCertPath(channel.getRootCertPath());
    initUnionPayConfig(config);
  }
  
  
  /**
   * 功能：后台交易提交请求报文并接收同步应答报文<br>
   * 
   * @param reqUrl
   *          请求地址<br>
   * @param encoding<br>
   * @return 应答http 200返回true ,其他false<br>
   */
  public static Map<String, String> get(String reqUrl, String encoding) {
    Map<String, String> rspData = new HashMap<String, String>();
    LogUtil.writeLog("请求银联地址:" + reqUrl);
    // 发送后台请求数据
    HttpClient hc = new HttpClient(reqUrl, 30000, 30000);
    try {
      int status = hc.sendGet(encoding);
      if (200 == status) {
        String resultString = hc.getResult();
        if (null != resultString && !"".equals(resultString)) {
          // 将返回结果转换为map
          Map<String, String> tmpRspData = SDKUtil.convertResultStringToMap(resultString);
          rspData.putAll(tmpRspData);
        }
      } else {
        LogUtil.writeLog("返回http状态码[" + status + "]，请检查请求报文或者请求地址是否正确");
      }
    } catch (Exception e) {
      LogUtil.writeErrorLog(e.getMessage(), e);
    }
    return rspData;
  }

}
